package com.wxg.gui.table;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.AbstractTableModel;

import com.wxg.json.CommandBeanJsonDB;
import com.wxg.json.bean.CommandBean;
import com.wxg.util.WinProcessUtils;

public class CommandTable {

	private JFrame jframe;
	
	private CommandBeanJsonDB tabledatas;
	
	private JTable jtable;
	
	private AbstractTableModel tableModel;
	
	private JButton btnAdd;
	
	private JButton btnRemove;
	
	private JButton btnStart;
	private JButton btnStop;
	
	private CommandDialog jde;
	
	public CommandTable(){
		tabledatas = new CommandBeanJsonDB();
		jframe = new JFrame();
	}
	
	private void addButtons(){
		/**
		 * 无参构造方法默认是采用的FlowLayout。
		 */
		JPanel northPanel = new JPanel();
		northPanel.setLayout(new BoxLayout(northPanel, BoxLayout.X_AXIS));
		
		btnAdd = new JButton("Add");
		btnRemove = new JButton("Remove");
		btnRemove.setEnabled(false);
		btnStart = new JButton("Start");
		btnStart.setEnabled(false);
		btnStop = new JButton("Stop");
		btnStop.setEnabled(false);
		
		northPanel.add(btnAdd);
		northPanel.add(btnRemove);
		northPanel.add(btnStart);
		northPanel.add(btnStop);
		jframe.add(northPanel, BorderLayout.NORTH);
	}
	
	private void addJTable(){
		tableModel = new MyTableModel();
		jtable = new JTable(tableModel);
		jframe.add(new JScrollPane(jtable), BorderLayout.CENTER);
	}
	
	private void initDialog(){
		if(null == jde){
			jde = new CommandDialog(jframe, tabledatas, tableModel);
			jde.init();
		}
		jde.show();
	}
	
	private void bindEvent4btnAdd(){
		/**
		 * 
		 */
		btnAdd.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				initDialog();
			}
		});
		
		/**
		 * 
		 */
		btnRemove.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				List<Integer> removeIndexs = getSelectIndexs();
				if(removeIndexs.size() == 1){
					tabledatas.remove(removeIndexs.get(0));
				}else{
					tabledatas.removes(removeIndexs.toArray(new Integer[]{}));
				}
				tableModel.fireTableDataChanged();
			}
		});
		
		btnStart.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				ListSelectionModel lsm = jtable.getSelectionModel();
				final int min = lsm.getMinSelectionIndex();
				Thread t = new Thread(new Runnable() {
					@Override
					public void run() {
						CommandBean bean = tabledatas.get(min);
						String command = bean.getCommand();
						String dir = bean.getDir();
						WinProcessUtils.startProcess(command, dir);
						tabledatas.start(min);
						lsm.clearSelection();
					}
				});
				t.setDaemon(true);
				t.start();
			}
		});
		
		btnStop.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				ListSelectionModel lsm = jtable.getSelectionModel();
				int min = lsm.getMinSelectionIndex();
				CommandBean bean = tabledatas.get(min);
				WinProcessUtils.stopProcess(bean.getPort());
				tabledatas.stop(min);
				lsm.clearSelection();
			}
		});
	}
	
	private void bindEvent4JTable(){
		jtable.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
			@Override
			public void valueChanged(ListSelectionEvent e) {
				ListSelectionModel lsm = (ListSelectionModel)e.getSource();
				//System.out.printf("lsm.isSelectionEmpty() : %b\n", lsm.isSelectionEmpty());
				if(lsm.isSelectionEmpty()){
					btnRemove.setEnabled(false);
					btnStart.setEnabled(false);
					btnStop.setEnabled(false);
				}else{
					btnRemove.setEnabled(true);
					if(isSingleSelected()){
						int i = lsm.getMinSelectionIndex();
						CommandBean bean = tabledatas.get(i);
						if(!bean.isAlive() && null!=bean.getPort() && !"0000".equals(bean.getPort())){
							btnStart.setEnabled(true);
						}
						if(bean.isAlive()){
							btnStop.setEnabled(true);
						}
					}
				}
			}
		});
	}
	
	public void init(){
		String title = "Hello JTable";
		jframe.setTitle(title);
		
		addButtons();
		addJTable();
		
		bindEvent4btnAdd();
		bindEvent4JTable();
		
		jframe.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
		jframe.setSize(500, 300);
		jframe.setVisible(true);
	}
	
	class MyTableModel extends AbstractTableModel{

		private static final long serialVersionUID = 1185810000761339461L;

		@Override
		public String getColumnName(int column) {
			return tabledatas.head()[column];
		}

		@Override
		public int getRowCount() {
			return tabledatas.data().length;
		}

		@Override
		public int getColumnCount() {
			return tabledatas.head().length;
		}

		@Override
		public Object getValueAt(int rowIndex, int columnIndex) {
			return tabledatas.data()[rowIndex][columnIndex];
		}
	}

	public JFrame getJframe() {
		return jframe;
	}

	public CommandBeanJsonDB getTabledatas() {
		return tabledatas;
	}

	private List<Integer> getSelectIndexs(){
		ListSelectionModel lsm = jtable.getSelectionModel();
		List<Integer> selectIndexs = new ArrayList<Integer>();
		int min = lsm.getMinSelectionIndex();
		int max = lsm.getMaxSelectionIndex();
		for(int i=min; i<=max; i++){
			if(lsm.isSelectedIndex(i)){
				selectIndexs.add(i);
			}
		}
		return selectIndexs;
	}
	
	private boolean isSingleSelected(){
		ListSelectionModel lsm = jtable.getSelectionModel();
		int min = lsm.getMinSelectionIndex();
		int max = lsm.getMaxSelectionIndex();
		if(min != max){
			return false;
		}else{
			return true;
		}
	}
	
}
